//
//  FSNoticSettingViewController.m
//  fanshu
//
//  Created by 许海峰 on 16/4/4.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSNoticSettingViewController.h"
#import "FSNoticeSettingItem.h"
#import "FSNoticeSettingSwitchCell.h"
#import "FSNoticeSettingIndicatorCell.h"
#import "FSCollectionNoticeViewController.h"
#import "FSSpecialTopicNoticeViewController.h"

@interface FSNoticSettingViewController ()
@property (nonatomic,strong) NSMutableArray *noticeSettingItemArray;
@end
static NSString * const IDSwitch = @"switchCell";
static NSString * const IDIndicator = @"indicatorCell";

@implementation FSNoticSettingViewController
- (NSMutableArray *)noticeSettingItemArray
{
    if (_noticeSettingItemArray == nil) {
        _noticeSettingItemArray = [NSMutableArray array];
    }
    return _noticeSettingItemArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"关注更新推送";
    self.navigationItem.rightBarButtonItem = nil;
    //加载网络数据
    [self loadWebData];
    
    [self.tableView registerClass:[FSNoticeSettingIndicatorCell class] forCellReuseIdentifier:IDIndicator];
    [self.tableView registerClass:[FSNoticeSettingSwitchCell class] forCellReuseIdentifier:IDSwitch];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noticeSettingItemSwitchChange" object:nil];
}
-(void)switchChange:(NSNotification *)note
{
    
    //取出数组第一个
    NSArray *section = self.noticeSettingItemArray[0];
    FSNoticeSettingItem *firstItem = section[0];
    //发出通知的item
    FSNoticeSettingItem *cellItem = note.object;
    NSLog(@"%@",cellItem.title);
    //如果是第一个item,且开关被打开
    if ([firstItem isEqual:cellItem] && cellItem.switchOn == NO) {
        [self loadWebData2];
    } else {
        [self loadWebData];
    }
    //延迟刷新,否则会造成UISwitch动画被打断
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
    });
}
-(void)loadWebData2
{
    [self.noticeSettingItemArray removeAllObjects];
    //模拟联网数据
    //第一组
    FSNoticeSettingItem *item0 = [[FSNoticeSettingItem alloc] init];
    item0.title = @"接收更新推送";
    item0.indicatorRight = NO;
    item0.switchOn = NO;
    NSArray *section0 = @[item0];
    [self.noticeSettingItemArray addObject:section0];
    
}
-(void)loadWebData
{
    //模拟联网数据
    [self.noticeSettingItemArray removeAllObjects];
    //第一组
    FSNoticeSettingItem *item0 = [[FSNoticeSettingItem alloc] init];
    item0.title = @"接收更新推送";
    item0.indicatorRight = NO;
    item0.switchOn = YES;
    NSArray *section0 = @[item0];
    [self.noticeSettingItemArray addObject:section0];
    
    
    //第二组
    FSNoticeSettingItem *item1 = [[FSNoticeSettingItem alloc] init];
    item1.title = @"默认打开新关注对象的更新推送";
    item1.indicatorRight = NO;
    item1.switchOn = YES;
    NSArray *section1 = @[item1];
    [self.noticeSettingItemArray addObject:section1];
    
    //第三组3个
    FSNoticeSettingItem *item2_0 = [[FSNoticeSettingItem alloc] init];
    item2_0.title = @"关注的用户";
    item2_0.indicatorRight = YES;
    item2_0.switchOn = YES;//无所谓,可不写
    item2_0.destinationName = @"FSUserNoticeViewController";
    
    FSNoticeSettingItem *item2_1 = [[FSNoticeSettingItem alloc] init];
    item2_1.title = @"关注的专题";
    item2_1.indicatorRight = YES;
    item2_1.destinationName = @"FSSpecialTopicNoticeViewController";
    
    FSNoticeSettingItem *item2_2 = [[FSNoticeSettingItem alloc] init];
    item2_2.title = @"关注的文集";
    item2_2.indicatorRight = YES;
    item2_2.destinationName = @"FSCollectionNoticeViewController";
    
    NSArray *section2 = @[item2_0,item2_1,item2_2];
    [self.noticeSettingItemArray addObject:section2];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.noticeSettingItemArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *itemArray = self.noticeSettingItemArray[section];
    return itemArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *section = self.noticeSettingItemArray[indexPath.section];
    FSNoticeSettingItem *item = section[indexPath.row];
    
    if (item.indicatorRight == NO) {
        FSNoticeSettingSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:IDSwitch];
        cell.item = item;
        //移除,重新监听
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noticeSettingItemSwitchChange" object:item];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchChange:) name:@"noticeSettingItemSwitchChange" object:item];
        return cell;
    }else{
        FSNoticeSettingIndicatorCell *cell = [tableView dequeueReusableCellWithIdentifier:IDIndicator];
        cell.item = item;
        return cell;
    }
}
//自定义HeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        return 50;
    } else {
        return 0;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, ScreenWidth-20, 10)];
        [header addSubview:titleLabel];
        titleLabel.text = @"管理更新推送对象";
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textColor = [UIColor orangeColor];
        
        UILabel *detialLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabel.frame), ScreenWidth-20, 40)];
        
        detialLabel.text = @"您可以在此分对象管理需要推送更新的项目,打开推送,则该关注对象一有文章更新,就会提示您";
        detialLabel.numberOfLines = 0;
        detialLabel.font = [UIFont systemFontOfSize:12];
        detialLabel.textColor = [UIColor darkGrayColor];
        [header addSubview:detialLabel];
        return header;
    } else {
        return nil;
    }
}

#pragma mark - Table view delegate
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *section = self.noticeSettingItemArray[indexPath.section];
    FSNoticeSettingItem *item = section[indexPath.row];
    if (item.indicatorRight) {
        return YES;
    }
    return NO;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%@",indexPath);
    NSArray *section = self.noticeSettingItemArray[indexPath.section];
    FSNoticeSettingItem *item = section[indexPath.row];
    UITableViewController *destVC = [[NSClassFromString(item.destinationName) alloc] init];
    
    [self.navigationController pushViewController:destVC animated:YES];
}
@end
